<?php
class HotItems extends CWidget
{
	public $id='slider';
	public $keyword= '';
	public $dirname = 'hotitems';
	public $filename = '';
	public $page_size = 5;
	public $express = 0;
	public $width = 248;
	public $height = 248;
	public $tip = 'bottom';

	protected $assetUrl;

	public function init(){
		$this->assetUrl = Yii::app()->assetManager->publish(dirname(__FILE__).'/asset');
		$this->regFiles();
		parent::init();
	}

	public function run(){
		$data = $this->getData();
		echo '<div class="slider" id="'.$this->id.'">
			<ul id="'.$this->id.'Content">';
		foreach($data as $d){
			$fulltitle = strip_tags($d['title']);
			$desc = mb_substr($fulltitle,0,6,'utf-8');
			$title = mb_substr($fulltitle,0,15,'utf-8');

			echo '<li class="sliderImage">
					<a href="'.$d['click_url'].'" target="_blank"><img src="'.$d['pic_url'].'" alt="'.$fulltitle.'" /></a>
					<span class="slider_'.$this->tip.'"><strong>'.$desc.'</strong><br /><a href="'.$d['click_url'].'" target="_blank">'.$title.'</a><em> ￥'.$d['price'].'</em></span>
				 </li>';
		}
		echo '	<div class="clear sliderImage"></div>
			</ul>		
		</div>';
	}

	protected function getData(){
		$config= array(
			'method' => 'taobao.taobaoke.items.get',
			'fields' => 'title,pic_url,price,click_url',
			'page_size' => $this->page_size,
			'sort' => 'commissionVolume_desc',
			'start_credit' => '1crown',
			'sevendays_return' => '1',
			'real_describe' => '1',
			'cid' => 0,
			'keyword' => $this->keyword,
			'outer_code'=>'miyifun',
		);

		$api = new TaobaoApi($config);
		$result = $api->getData($this->express, $this->dirname, $this->filename);
		return $result['taobaoke_items_get_response']['taobaoke_items']['taobaoke_item'];

	}

	protected function regFiles(){
		$cs = Yii::app()->clientScript;
		$cs->registerCoreScript('jquery');
		$cs->registerScriptFile($this->assetUrl.'/js/s3Slider.js');
		$cs->registerCssFile($this->assetUrl.'/css/style.css');
		$cs->registerCss($this->id,"
			#".$this->id." {
				width: ".$this->width."px;
				height: ".$this->height."px;
				position: relative; 
				overflow: hidden; 
			}
			#".$this->id."Content {
				width: ".$this->width."px;
				height: ".$this->height."px;
				position: absolute;
				top: 0;
				margin-left: 0;
			}
			.sliderImage{height:".$this->height."px;}
			.sliderImage span{
				width: ".$this->width."px;
			}
			.sliderImage a,.sliderImage a img{height:".$this->height."px;width:".$this->width."px;border:none!important;}
		");
		$cs->registerScript($this->id,"
			$('#".$this->id."').s3Slider({
				timeOut:3000
			});
		",CClientScript::POS_READY);
	}
}
